
#include "com_win.h"
#include "page_manager.h"


//********************************************************* */
static com_win_t cwin;

static void com_win_event_callback(lv_event_t * e)
{
    lv_point_t vect;
    static lv_point_t pe;
    static lv_point_t ps;
    lv_indev_t* indev = NULL;

    lv_coord_t x = 0;
    lv_coord_t y = 0;
    lv_obj_t * obj = NULL;

    indev = lv_indev_get_act();
    if(indev == NULL) return;

    obj = lv_event_get_target(e);
    if(obj == NULL) return;

    lv_indev_get_vect(indev, &vect);

    switch(e->code){
    case LV_EVENT_CLICKED:
    {

        break;
    }
    case LV_EVENT_PRESSED:
    {
        lv_indev_get_point(indev, &ps);
        break;
    }
    case LV_EVENT_PRESSING:
    {
        x = lv_obj_get_x(obj) + vect.x;
        lv_obj_set_x(obj,x);

        break;
    }
    case LV_EVENT_RELEASED:
    {

        break;
    }
    default:break;
    }
}
static int com_win_ui_create(lv_obj_t *parent)
{
    cwin.bg = lv_obj_create(parent);

    lv_obj_set_pos(cwin.bg,0,0);
    lv_obj_set_size(cwin.bg,480,320);

	lv_obj_set_style_radius(cwin.bg, 0, LV_PART_MAIN);
	lv_obj_set_style_border_width(cwin.bg, 0, LV_PART_MAIN);
	lv_obj_set_style_border_color(cwin.bg, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);
	lv_obj_set_style_border_opa(cwin.bg, LV_OPA_0, LV_PART_MAIN);

	lv_obj_set_style_outline_width(cwin.bg, 0, LV_PART_MAIN);
	lv_obj_set_style_shadow_width(cwin.bg, 0, LV_PART_MAIN);
	lv_obj_set_scrollbar_mode(cwin.bg, LV_SCROLLBAR_MODE_OFF);
	lv_obj_set_style_pad_all(cwin.bg, 0,0);

    #if 0
    lv_obj_set_style_bg_opa(cwin.bg,LV_OPA_0,0);
    #else
    lv_obj_set_style_bg_opa(cwin.bg,LV_OPA_100,0);
    lv_obj_set_style_bg_color(cwin.bg,lv_color_make(0x26,0x7a,0xff),0);
    #endif

    lv_obj_add_event_cb(cwin.bg,com_win_event_callback,LV_EVENT_ALL,0);

    return 0;
}

static int com_win_ui_destroy(void)
{
    if(cwin.bg){
        lv_obj_del(cwin.bg);
        cwin.bg = NULL;
    }

    return 0;
}

static void com_win_on_custom_attr_config(page_base_t* self)
{
    debug_d("attr cfg\n");
    self->priv.anim.attr.type  = LOAD_ANIM_MOVE_RIGHT;
    self->priv.anim.attr.time  = 5000;
    self->priv.anim.attr.path  = lv_anim_path_ease_in_out;
}
static void com_win_on_view_load(page_base_t* self)
{

}
static void com_win_on_view_did_load(page_base_t* self)
{
    debug_d("load\n");
    com_win_ui_create(self->root);
}
static void com_win_on_view_will_appear(page_base_t* self)
{

}
static void com_win_on_view_did_appear(page_base_t* self)
{

}
static void com_win_on_view_will_disappear(page_base_t* self)
{

}
static void com_win_on_view_did_disappear(page_base_t* self)
{

}
static void com_win_on_view_did_unload(page_base_t* self)
{
    debug_d("unload\n");
    com_win_ui_destroy();
}

static page_vtable_t com_win_page_param =
{
    .on_custom_attr_config  = com_win_on_custom_attr_config,
    .on_view_load           = com_win_on_view_load,
    .on_view_did_load       = com_win_on_view_did_load,
    .on_view_will_appear    = com_win_on_view_will_appear,
    .on_view_did_appear     = com_win_on_view_did_appear,
    .on_view_will_disappear = com_win_on_view_will_disappear,
    .on_view_did_disappear  = com_win_on_view_did_disappear,
    .on_view_did_unload     = com_win_on_view_did_unload,
};


page_vtable_t* com_win_page_create(void)
{
    return &com_win_page_param;
}
